/* 
 * File:   main.c
 * Author: jorisgirardot
 *
 * Created on 14 novembre 2012, 22:48
 */


#include "main.h"
/*
 * 
 */
int main(int argc, char** argv) {
    
    Graph graph;
    
    if(argc == 2)
    {
        readCSVFile(argv[1], &graph);
        graph_printGraphInfo(&graph);
        
        displayMenu(&graph);
    }
    else
    {
        //readCSVFile("matrice.csv", &graph);      
        generateRandomMatrix(&graph);
        graph_printGraphInfo(&graph);
        
        displayMenu(&graph);
    }

    return (EXIT_SUCCESS);
}

void displayMenu(Graph *graph)
{
    int status;
    Matrix warshall;
    int option1 = 0, option2 = 0;
    do {
        printf("1 - Ajouter un noeud\n"
                "2 - Supprimer un noeud\n"
                "3 - Ajouter une arête\n"
                "4 - Supprimer une arête\n"
                "5 - Warshall\n"
                "6 - BFS\n"
                "7 - DFS\n"
		"-- Nouveau depuis le 17/12 --"
                "8 - Malgrange\n"
		"9 - Connexe\n"
		"10 - Euler\n"
                "99 - Quitter\n");
        while (scanf("%d", &status) != 1) {
            clear_stream(stdin);
            printf("Valeur invalide. Réessayer : ");
            fflush(stdout);
        }
        switch (status){
            case 1 :
                graph_addNode(graph);
                printMatrix(&graph->adjacency);
                break;
            case 2 :
                printf("\nNumero du sommet :");
                scanf("%d", &option1);
                printf("\n");
                graph_removeNode(option1,graph);
                printMatrix(&graph->adjacency);
                break;
            case 3 :
                printf("\nSommet 1 :");
                scanf("%d", &option1);
                printf("\nSommet 2 :");
                scanf("%d", &option2);
                graph_addVertex(option1,option2,graph);
                printMatrix(&graph->adjacency);
                break;
            case 4 : 
                printf("\nSommet 1 :");
                scanf("%d", &option1);
                printf("\nSommet 2 :");
                scanf("%d", &option2);
                graph_removeVertex(option1,option2,graph);
                printMatrix(&graph->adjacency);
                break;
            case 5 :
                warshall = graph_Warshall(*graph);
                printMatrix(&warshall);
                break;
            case 6 :
                main_BFS(*graph);
                break;
            case 7 :
                main_DFS(*graph);
                break;
            case 8 :
		afficherMalgrange(&graph->adjacency);
                break;
	    case 9 :
		if( isConnected(&graph->adjacency) == 1 ) {printf("LE GRAPHE EST CONNEXE\n\n");} else {printf("LE GRAPHE N'EST PAS CONNEXE\n\n");};
                break;
	    case 10 :
		euler(&graph->adjacency);
                break;
        }
    }
    while(status!=99);
}

void main_BFS(Graph graph)
{
    int * visited = (int *)malloc(graph.nodes * sizeof(int));
    graph_BFS(graph.adjacency.table, visited, 0, graph.nodes);
    printf("\n\n");
}

void main_DFS(Graph graph)
{
    int *queue = (int *)malloc(sizeof(int)*graph.nodes);
    printDFS(graph.adjacency.table, graph.nodes,queue);
    printf("\n\n");
}
